package com.sgb.kjwl.viewmodel.self.carMakers;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.OwnPathEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.activity.self.carMaster.OwnPathsActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/6/18
 */
public class OwnPathsModel extends AndroidViewModel {
    public MutableLiveData<List<OwnPathEntity>> pathEntities = new MutableLiveData<>();
    public MutableLiveData<Boolean> deleteLD = new MutableLiveData<>();

    public OwnPathsModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取我的路线
     */
    public void getMySubPath(OwnPathsActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getMyPaths("10", "1").enqueue(new Callback<BaseEntity<ListResponse<List<OwnPathEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<OwnPathEntity>>>> call, Response<BaseEntity<ListResponse<List<OwnPathEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    pathEntities.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<OwnPathEntity>>>> call, Throwable t) {

            }
        });
    }

    /**
     * 删除某条路线
     */
    public void deletePath(OwnPathsActivity activity, String id) {
        HashMap map = new HashMap();
        map.put("id", id);
        map.put("status", true);
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().settingPath(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "删除成功");
                deleteLD.postValue(true);
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });

    }
}
